package com.yxplearn.mybatisplus.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yxplearn.mybatisplus.autoconfig.MyYmlAutoConfiguration;
import com.yxplearn.mybatisplus.mapper.UserMapper;
import com.yxplearn.mybatisplus.util.JdbcUtil;
import com.yxplearn.mybatisplus.util.asyn.AsynManager;
import com.yxplearn.mybatisplus.util.asyn.DbTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yxplearn.mybatisplus.manager.GameExplorer;
import com.yxplearn.mybatisplus.pojo.User;
import com.yxplearn.mybatisplus.pojo.Users;

@Controller
public class MybatisTestController {

    @Autowired
    AsynManager asynManager;

    @Autowired
    UserMapper userMapper;

    @Autowired
    MyYmlAutoConfiguration configuration;

    @RequestMapping("/mybatisplus")
    @ResponseBody
    public Map<Long, User> testSelectAllUser() {
        System.out.println("1" + new Date());
        asynManager.addTask(new DbTask() {
            @Override
            public void run() {
                System.out.println("2" + new Date());
                Users users = GameExplorer.getEntity(1, Users.class);
                Map<Long, User> userList = users.getUsers();
                for (Entry<Long, User> element : userList.entrySet()) {
                    System.out.println(element.getKey() + " " + element.getValue() + " " + new Date());
                }
            }
        });
//		Users users = GameExplorer.getEntity(1, Users.class);
//		Map<Long, User> userList = users.getUsers();
//		for (Entry<Long, User> element : userList.entrySet()) {
//			System.out.println(element.getKey() + " " + element.getValue());
//		}
        return new HashMap<>();
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Map<Long, User> insertAllUser() {
        Users users = GameExplorer.getEntity(1, Users.class);
        Map<Long, User> userList = users.getUsers();
        for (Entry<Long, User> element : userList.entrySet()) {
            Map<Integer, Integer> tt = new HashMap<>();
            tt.putIfAbsent(1, 4);
            String ttStr = JSON.toJSONString(tt);
            element.getValue().setAttr(tt);
            System.out.println(element.getKey() + " " + element.getValue());
            userMapper.insert(element.getValue());
        }
        return new HashMap<>();
    }

    @RequestMapping("/insertUser")
    @ResponseBody
    public Map<Long, User> insertUser() {
        System.out.println(Users.class.hashCode());
        System.out.println(Users.class.hashCode());
        System.out.println(Users.class.hashCode());


        User user = new User();
        Map map = new HashMap();
        map.putIfAbsent("ff", "234");
        user.setAttr(map);
        user.setAttrJson(map);
        userMapper.insert(user);
        return new HashMap<>();
    }

    @RequestMapping("/ymlTest")
    @ResponseBody
    public MyYmlAutoConfiguration ymlTest() {
        return configuration;
    }

    /**
     * 更新指定字段
     *
     * @return
     */
    @RequestMapping("/updateSpecialField")
    @ResponseBody
    public String updateSpecialField() throws IllegalAccessException, NoSuchFieldException {
        User user = userMapper.selectById(1);
        user.setName("ffasdfds");
        JdbcUtil.ins.updateEntityPartParam(userMapper, user, "name");
        return "ok";
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public String updateUser() {
        Users users = GameExplorer.getEntity(1, Users.class);
        Map<Long, User> userList = users.getUsers();
        User user = userList.get(Long.valueOf(1));
        user.setName("xxxxppp");
//        userMapper.updateById(user);
        userMapper.update(user, new UpdateWrapper<User>().eq("id", 1));
        return "ok";
    }

}
